package com.controller;

import com.bean.*;
import com.dao.*;
import com.model.SchoolModel;
import com.model.ShowModel;
import com.model.UserModel;
import com.model.VoluntaryModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.*;

@Controller
@SessionAttributes({"schoolList","showlist"})
public class WriteController {

    @Autowired
    private high_school_infoMapper highSchoolInfoMapper;

    @Autowired
    private voluntary_infoMapper voluntaryInfoMapper;

    @Autowired
    private voluntary_detail_infoMapper voluntaryDetailInfoMapper;

    @Autowired
    private key_sequenceMapper keySequenceMapper;

    @Autowired
    private high_school_major_infoMapper highSchoolMajorInfoMapper;

    @RequestMapping("/tian_bao")
    public String tian_bao(ModelMap map, HttpServletRequest request){
        ArrayList<SchoolModel> arrayList = new ArrayList<>();
        HttpSession session = request.getSession();
        if(session.getAttribute("loginUser") == null){
            return "init";
        }
        UserModel userModel = (UserModel) session.getAttribute("loginUser");
        int score = userModel.getScore();
        high_school_infoExample example = new high_school_infoExample();
        high_school_infoExample.Criteria criteria = example.createCriteria();
        criteria.andSCH_LINELessThanOrEqualTo(score);
        List<high_school_info> list = highSchoolInfoMapper.selectByExample(example);
        if(!list.isEmpty()){
            for(int i=0;i<list.size();i++){
                SchoolModel model = new SchoolModel();
                model.setSch_id(list.get(i).getSCH_ID());
                model.setSch_name(list.get(i).getSCH_NAME());
                arrayList.add(model);
            }
        }
        map.put("schoolList",arrayList);
        return "tian_bao";
    }

    @RequestMapping("/vol_submit")
    @ResponseBody
    public String vol_sub(VoluntaryModel voluntaryModel,HttpServletRequest request){
        HttpSession session = request.getSession();
        UserModel user = (UserModel) session.getAttribute("loginUser");
        voluntary_infoExample example = new voluntary_infoExample();
        voluntary_infoExample.Criteria criteria = example.createCriteria();
        criteria.andSTU_IDEqualTo(user.getStu_id());
        List<voluntary_info> list = voluntaryInfoMapper.selectByExample(example);
        if(!list.isEmpty()){
            return "failure";
        }
        ArrayList arrayList = new ArrayList();
        for(int i = 0;i<voluntaryModel.getMajor_id().length;i++){
            if(voluntaryModel.getMajor_id()[i] == 0){
                continue;
            }
            else{
                arrayList.add(voluntaryModel.getMajor_id()[i]);
            }
        }
        ArrayList arrayList2 = new ArrayList();
        for(int i = 0;i<voluntaryModel.getMajor_id().length;i++){
            if(voluntaryModel.getMajor_id()[i] == 0){
                continue;
            }
            else{
                arrayList2.add(voluntaryModel.getMajor_id2()[i]);
            }
        }
        ArrayList arrayList3 = new ArrayList();
        for(int i = 0;i<voluntaryModel.getMajor_id().length;i++){
            if(voluntaryModel.getMajor_id()[i] == 0){
                continue;
            }
            else{
                arrayList3.add(voluntaryModel.getMajor_id3()[i]);
            }
        }
        HashSet set = new HashSet(arrayList);
        HashSet set2 = new HashSet(arrayList2);
        HashSet set3 = new HashSet(arrayList3);
        if(set.size() != arrayList.size()){
            return "false";
        }
        if(set2.size() != arrayList2.size()){
            return "false2";
        }
        if(set3.size() != arrayList3.size()){
            return "false3";
        }
        int x[] = new int[3];
        for(int i=0;i<3;i++){
            key_sequenceExample example1 = new key_sequenceExample();
            key_sequenceExample.Criteria criteria1 = example1.createCriteria();
            criteria1.andSEQ_IDEqualTo("VOL_ID");
            List<key_sequence> keyList = keySequenceMapper.selectByExample(example1);
            voluntary_info voluntaryInfo = new voluntary_info();
            voluntaryInfo.setSCH_ID(voluntaryModel.getSch_id()[i]);
            voluntaryInfo.setSTU_ID(user.getStu_id());
            int vol_id = keyList.get(0).getNOW_SEQUENCE()+1;
            voluntaryInfo.setVOL_ID(vol_id);
            Date date = new Date();
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String now = format.format(date);
            voluntaryInfo.setVOL_TIME(now);
            voluntaryInfoMapper.insertSelective(voluntaryInfo);
            x[i] = keyList.get(0).getNOW_SEQUENCE()+1;
            key_sequence keySequence = new key_sequence();
            keySequence.setNOW_SEQUENCE(keyList.get(0).getNOW_SEQUENCE()+1);
            keySequenceMapper.updateByExampleSelective(keySequence,example1);
        }
        for(int i=0;i<3;i++){
            key_sequenceExample example1 = new key_sequenceExample();
            key_sequenceExample.Criteria criteria1 = example1.createCriteria();
            criteria1.andSEQ_IDEqualTo("DETAIL_ID");
            List<key_sequence> keyList = keySequenceMapper.selectByExample(example1);
            voluntary_detail_info voluntaryDetailInfo = new voluntary_detail_info();
            voluntaryDetailInfo.setDETAIL_ID(keyList.get(0).getNOW_SEQUENCE()+1);
            if(i==0) {
                for (int j = 0; j < arrayList.size(); j++) {
                    if (j == 0) {
                        voluntaryDetailInfo.setMAJOR_ID1(Integer.parseInt(arrayList.get(j).toString()));
                    } else if (j == 1) {
                        voluntaryDetailInfo.setMAJOR_ID2(Integer.parseInt(arrayList.get(j).toString()));
                    } else if (j == 2) {
                        voluntaryDetailInfo.setMAJOR_ID3(Integer.parseInt(arrayList.get(j).toString()));
                    } else if (j == 3) {
                        voluntaryDetailInfo.setMAJOR_ID4(Integer.parseInt(arrayList.get(j).toString()));
                    } else if (j == 4) {
                        voluntaryDetailInfo.setMAJOR_ID5(Integer.parseInt(arrayList.get(j).toString()));
                    }
                }
            }
            else if(i==1){
                for(int j=0;j<arrayList2.size();j++){
                    if(j==0){
                        voluntaryDetailInfo.setMAJOR_ID1(Integer.parseInt(arrayList2.get(j).toString()));
                    }
                    else if(j==1){
                        voluntaryDetailInfo.setMAJOR_ID2(Integer.parseInt(arrayList2.get(j).toString()));
                    }
                    else if(j==2){
                        voluntaryDetailInfo.setMAJOR_ID3(Integer.parseInt(arrayList2.get(j).toString()));
                    }
                    else if(j==3){
                        voluntaryDetailInfo.setMAJOR_ID4(Integer.parseInt(arrayList2.get(j).toString()));
                    }
                    else if(j==4){
                        voluntaryDetailInfo.setMAJOR_ID5(Integer.parseInt(arrayList2.get(j).toString()));
                    }
                }
            }
            else{
                for(int j=0;j<arrayList3.size();j++){
                    if(j==0){
                        voluntaryDetailInfo.setMAJOR_ID1(Integer.parseInt(arrayList3.get(j).toString()));
                    }
                    else if(j==1){
                        voluntaryDetailInfo.setMAJOR_ID2(Integer.parseInt(arrayList3.get(j).toString()));
                    }
                    else if(j==2){
                        voluntaryDetailInfo.setMAJOR_ID3(Integer.parseInt(arrayList3.get(j).toString()));
                    }
                    else if(j==3){
                        voluntaryDetailInfo.setMAJOR_ID4(Integer.parseInt(arrayList3.get(j).toString()));
                    }
                    else if(j==4){
                        voluntaryDetailInfo.setMAJOR_ID5(Integer.parseInt(arrayList3.get(j).toString()));
                    }
                }
            }
            voluntaryDetailInfo.setVOL_ID(x[i]);
            voluntaryDetailInfoMapper.insertSelective(voluntaryDetailInfo);
            key_sequence keySequence = new key_sequence();
            keySequence.setNOW_SEQUENCE(keyList.get(0).getNOW_SEQUENCE()+1);
            keySequenceMapper.updateByExampleSelective(keySequence,example1);
        }
        return "success";
    }

    @RequestMapping("/show_vol")
    public String showDetail(ModelMap map,HttpServletRequest request){
        HttpSession session = request.getSession();
        UserModel user = (UserModel) session.getAttribute("loginUser");
        if(user != null){
            int stu_id = user.getStu_id();
            voluntary_infoExample example1 = new voluntary_infoExample();
            voluntary_infoExample.Criteria criteria1 = example1.createCriteria();
            criteria1.andSTU_IDEqualTo(stu_id);
            List<voluntary_info> voluntary_infos = voluntaryInfoMapper.selectByExample(example1);
            int sch_id[] = new int[voluntary_infos.size()];//高校编号
            String sch_names[] = new String[voluntary_infos.size()];
            ArrayList<String[]> stringList = new ArrayList<>();
            if(!voluntary_infos.isEmpty()){
                for(int i=0;i<voluntary_infos.size();i++){
                    high_school_infoExample example = new high_school_infoExample();
                    high_school_infoExample.Criteria criteria = example.createCriteria();
                    criteria.andSCH_IDEqualTo(voluntary_infos.get(i).getSCH_ID());
                    sch_id[i] = voluntary_infos.get(i).getSCH_ID();
                    List<high_school_info> list = highSchoolInfoMapper.selectByExample(example);
                    sch_names[i] = list.get(0).getSCH_NAME();
                    voluntary_detail_infoExample detailInfoExample = new voluntary_detail_infoExample();
                    voluntary_detail_infoExample.Criteria criteria2 = detailInfoExample.createCriteria();
                    criteria2.andVOL_IDEqualTo(voluntary_infos.get(i).getVOL_ID());
                    List<voluntary_detail_info> detail_infos = voluntaryDetailInfoMapper.selectByExample(detailInfoExample);
                    for(int j=0;j<detail_infos.size();j++){
                        ArrayList alist = new ArrayList();
                        if(detail_infos.get(j).getMAJOR_ID5() != null){
                            alist.add(detail_infos.get(j).getMAJOR_ID1());
                            alist.add(detail_infos.get(j).getMAJOR_ID2());
                            alist.add(detail_infos.get(j).getMAJOR_ID3());
                            alist.add(detail_infos.get(j).getMAJOR_ID4());
                            alist.add(detail_infos.get(j).getMAJOR_ID5());
                        }
                        else if(detail_infos.get(j).getMAJOR_ID4() != null){
                            alist.add(detail_infos.get(j).getMAJOR_ID1());
                            alist.add(detail_infos.get(j).getMAJOR_ID2());
                            alist.add(detail_infos.get(j).getMAJOR_ID3());
                            alist.add(detail_infos.get(j).getMAJOR_ID4());
                        }
                        else if(detail_infos.get(j).getMAJOR_ID3() != null){
                            alist.add(detail_infos.get(j).getMAJOR_ID1());
                            alist.add(detail_infos.get(j).getMAJOR_ID2());
                            alist.add(detail_infos.get(j).getMAJOR_ID3());
                        }
                        else if(detail_infos.get(j).getMAJOR_ID2() != null){
                            alist.add(detail_infos.get(j).getMAJOR_ID1());
                            alist.add(detail_infos.get(j).getMAJOR_ID2());
                        }
                        else {
                            alist.add(detail_infos.get(j).getMAJOR_ID1());
                        }
                        String[] s = new String[5];
                        for(int k=0;k<alist.size();k++){
                            high_school_major_infoExample example2 = new high_school_major_infoExample();
                            high_school_major_infoExample.Criteria majorCri = example2.createCriteria();
                            majorCri.andMAJOR_IDEqualTo(Integer.parseInt(alist.get(k).toString()));
                            List<high_school_major_info> majorInfos = highSchoolMajorInfoMapper.selectByExample(example2);
                            s[k] = majorInfos.get(0).getMAJOR_NAME();
                        }
                        stringList.add(s);
                    }
                }
            }
            ShowModel model = new ShowModel();
            model.setSch_id(sch_id);
            model.setSch_names(sch_names);
            model.setMajors(stringList);
            map.put("showlist",model);
        }
        return "show";
    }
}
